// pages/auth/register/register.js
var api=require('../../../config/api');
var check=require('../../../utils/check.js');
var app = getApp();
Page({

    data: {
        username:'',
        password:'',
        confirmPassword:'',
        mobile:'',
        code:''
    },
    requestRegister: function (wxCode) {
        let that = this;
        wx.request({
          url: api.AuthRegister,
          data: {
            username: that.data.username,
            password: that.data.password,
            mobile: that.data.mobile,
            code: that.data.code,
            wxCode: wxCode
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            if (res.data.errno == 0) {
              app.globalData.hasLogin = true;
              wx.setStorageSync('userInfo', res.data.data.userInfo);
              wx.setStorage({
                key: "token",
                data: res.data.data.token,
                success: function () {
                  wx.switchTab({
                    url: '/pages/user/index/index'
                  });
                }
              });
            } else {
              wx.showModal({
                title: '错误信息',
                content: res.data.errmsg,
                showCancel: false
              });
            }
          }
        });
      },
    // 校验规则——————————————————
    startRegister(){
        var that=this;
        if(this.data.password.length<6||this.data.username.length<6){
            wx.showModal({
                title:'错误信息',
                content:'用户名和密码不得少于6位',
                showCancel:false
            });
            return false;
        }
        if(this.data.password !=this.data.confirmPassword){
            wx.showModal({
                title:'错误信息',
                content:'确认密码不一致',
                showCancel:false
            });
            return false;
        }
        if(this.data.mobile.length==0||this.data.code.length==0){
            wx.showModal({
                title:'错误信息',
                content:'手机号和验证码不能为空',
                showCancel:false
            });
            return false;
        }
        if (!check.isValidPhone(this.data.mobile)) {
            wx.showModal({
              title: '错误信息',
              content: '手机号输入不正确',
              showCancel: false
            });
            return false;
          }
        wx.login({
            success: function (res) {
              if (!res.code) {
                wx.showModal({
                  title: '错误信息',
                  content: '注册失败',
                  showCancel: false
                });
              }
              that.requestRegister(res.code);
            }
          });
    },

    bindUsernameInput(e){
        // console.log(e);
        this.setData({
            username:e.detail.value
        })
        // console.log(this.data.username);
    },
    bindPasswordInput(e){
        this.setData({
            password:e.detail.value
        })
    },
    bindConfirmPasswordInput(e){
        this.setData({
            confirmPassword:e.detail.value
        })
    },
    bindMobileInput(e){
        this.setData({
            mobile:e.detail.value
        })
    },
    bindCodeInput(e){
        this.setData({
            code:e.detail.value
        })
    },
    clearInput(e){
        console.log(e);
        switch (e.currentTarget.id) {
            case 'clear-username':
                this.setData({
                    username: ''
                });
                break;
            case 'clear-password':
                this.setData({
                    password: ''
                });
                break;
            case 'clear-confirm-password':
                this.setData({
                    confirmPassword: ''
                });
                break;
            case 'clear-mobile':
                this.setData({
                    mobile: ''
                });
                break;
            case 'clear-code':
                this.setData({
                    code: ''
                });
                break;
        }
    }





})